//
//  EMLoanViewController.m
//  EasyMoney
//
//  Created by zh on 2019/12/14.
//  Copyright © 2019 zh. All rights reserved.
//

#import "EMLoanViewController.h"
#import "EMWebViewController.h"
#import "EMVerifyCodeRequestModel.h"
#import "EMVerifyCodeResponseModel.h"
#import "EMLoginRequestModel.h"
#import "EMLoanHeaderView.h"
#import "EMLoadTableHeaderView.h"
#import "EMLoanProductTableViewCell.h"
#import "EMLoanDescTableViewCell.h"
#import "EMLoanRequestModel.h"
#import "EMLoanResponseModel.h"
#import "EMLocationManager.h"

@interface EMLoanViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) EMLoanHeaderView *headView;
@property (nonatomic,strong) EMLoadTableHeaderView *tableHeaderView;
@property (nonatomic,strong) EMLoanResponseModel *dataModel;

@end

@implementation EMLoanViewController

- (void)dealloc{
    LOG_MARK;
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
    [[EMLocationManager sharedManager] start];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //1:缓存数据获取
    EMLoanResponseModel *localModel = [[EMCacheMananger shared]getLocalData:@"LoanProduct"];
    if (localModel) {
        self.dataModel = localModel;
    }

    [self setupUI];
}

#pragma mark -api
- (void)requestData{

    EMLoanRequestModel *request = [[EMLoanRequestModel alloc]init];
    [request requestSuccess:^(EMLoanResponseModel *response, NSString * _Nonnull message) {
        [self.tableView.mj_header endRefreshing];
        if (response) {
            self.dataModel = response;
            self.tableHeaderView.model = response;
            [self.tableView reloadData];
            //更新缓存
            [[EMCacheMananger shared]updateLocalData:response andModelName:@"LoanProduct"];
        }
    } failure:^(NSInteger errorCode, NSString * _Nonnull errorMessage, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark -UI
- (void)setupUI{

    self.view.backgroundColor = [UIColor clearColor];
    self.headView = [[EMLoanHeaderView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(EMStatusBarHeight+44+123);
    }];

    self.tableHeaderView = [[EMLoadTableHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 306)];
    [self.tableHeaderView.contentBtn addTarget:self action:@selector(clickHeaderContentBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 150;
    self.tableView.tableHeaderView = self.tableHeaderView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.mas_equalTo(EMStatusBarHeight+44);
    }];

    [self.tableView registerClass:[EMLoanProductTableViewCell class] forCellReuseIdentifier:NSStringFromClass([EMLoanProductTableViewCell class])];
    [self.tableView registerClass:[EMLoanDescTableViewCell class] forCellReuseIdentifier:NSStringFromClass([EMLoanDescTableViewCell class])];

    EMRefreshHeader *header = [[EMRefreshHeader alloc] init];
    [header setRefreshingTarget:self refreshingAction:@selector(beginRefreshHeader)];
    self.tableView.mj_header = header;
}

#pragma mark - header
- (void)clickHeaderContentBtn:(UIButton *)sender
{
    ICEOpenUrl(self.dataModel.productInfo.buttonUrl);
}

- (void)beginRefreshHeader
{
    [self requestData];
}

#pragma mark - TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        EMLoanProductTableViewCell *productCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([EMLoanProductTableViewCell class]) forIndexPath:indexPath];
        productCell.productURL = self.dataModel.advertisementUrl;
        return productCell;
    }

    EMLoanDescTableViewCell *descCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([EMLoanDescTableViewCell class]) forIndexPath:indexPath];
    descCell.qaTextString = self.dataModel.QAText;

    return descCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0) {
        ICEOpenUrl(self.dataModel.advertisementUrl);
    }else if (indexPath.row == 1){
        ICEOpenUrl(self.dataModel.QAurl);
    }

}
@end
